﻿using Microsoft.SharePoint.Administration;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SharePoint.Security.ConfigAnalyzer.Engine.Common
{
    public static class Helper
    {
        public static bool OnSystemDrive(string location)
        {
            if (location.Contains("%"))
            {
                var result = location.Split("%".ToArray());

                if (result.Count() == 3)
                {
                    location = string.Format("{0}{1}", Environment.GetEnvironmentVariable(result[1]), result[2]);
                }

            }

            var systemDriveLetter = Environment.SystemDirectory.Split(":".ToArray())[0];
            var loactionDriveLetter = location.Split(":".ToArray())[0];

            return (loactionDriveLetter.Equals(systemDriveLetter, StringComparison.InvariantCultureIgnoreCase));
        }

        public static List<SPWebApplication> GetWebApps()
        {
            var webApps = new List<SPWebApplication>();

            SPFarm farm = ObjectHelper.Farm;
            foreach (SPService objService in farm.Services)
            {
                if (objService is SPWebService)
                {
                    SPWebService oWebService = (SPWebService)objService;
                    foreach (SPWebApplication oWebApplication in oWebService.WebApplications)
                    {
                        webApps.Add(oWebApplication);
                    }
                }
            }

            return webApps;
        }
    }
}
